<?php

namespace app\controller\user\controller;

use app\popo\dto\ShoppingCartDTO;
use app\service\ShoppingCartService;
use app\support\ResponseHelper;
use DI\Attribute\Inject;
use Shayvmo\WebmanAnnotations\Annotations\DeleteMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use support\Response;
use Webman\Http\Request;

#[RestController("/user/shoppingCart")]
class ShoppingCartController
{
    #[Inject]
    protected ShoppingCartService $shoppingCartService;

    /**
     * 添加购物车
     * @param Request $request
     * @return Response
     */
    #[PostMapping("/add")]
    public function add(Request $request): Response
    {
        $shoppingCartDTO = new ShoppingCartDTO($request);
        $this->shoppingCartService->addShoppingCart($shoppingCartDTO);
        return ResponseHelper::successJson("", "添加购物车成功");
    }

    /**
     * 查看购物车
     * @return Response
     */
    #[GetMapping("/list")]
    public function list(): Response
    {
        $list = $this->shoppingCartService->showShoppingCart();
        return ResponseHelper::successJson($list);
    }

    /**
     * 清空购物车
     * @return Response
     */
    #[DeleteMapping("/clean")]
    public function clean(): Response
    {
        $this->shoppingCartService->cleanShoppingCart();
        return ResponseHelper::successJson("", "清空购物车成功");
    }

    /**
     * 删除购物车中一个商品
     * @param Request $request
     * @return Response
     */
    #[PostMapping("/sub")]
    public function sub(Request $request): Response
    {
        $shoppingCartDTO = new ShoppingCartDTO($request);
        $this->shoppingCartService->subShoppingCart($shoppingCartDTO);
        return ResponseHelper::successJson("", "删除商品成功");
    }
}